package park.manage;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.zip.DataFormatException;


public class ReadFile {
    public static void readFile(TreeSet<Car> set) throws FileNotFoundException, ParseException {
        File file = new File("src/park/manage/Car_Information.txt");
        Scanner sc = new Scanner(file, StandardCharsets.UTF_8.toString());

        // 使用 Locale.ENGLISH 确保月份和星期几名称正确解析
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); // 明确指定时区为 GMT+08:00

        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] str = line.split(",");
            if(str.length==3){
                String name = str[0].trim();
                String carNumber = str[1].trim();
                String dateStr = str[2].trim();
                java.util.Date date = dateFormat.parse(dateStr);
                set.add(new Car(name,carNumber,date));
            }
        }
        //释放资源
        sc.close();
    }

    public static void readFile(HashSet<User> set ) throws FileNotFoundException {
        File file = new File("src/park/manage/User.txt");
        Scanner sc = new Scanner(file, StandardCharsets.UTF_8.toString());
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] str = line.split(",");
            if(str.length==2){
                String userName = str[0].trim();
                String password = str[1].trim();
                set.add(new User(userName,password));
            }
        }
        //释放资源
        sc.close();
    }
    public static void readFile(HashSet<Manager> set ,String path) throws FileNotFoundException {
        File file = new File(path);
        Scanner sc = new Scanner(file, StandardCharsets.UTF_8.toString());
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] str = line.split(",");
            if(str.length==2){
                String userName = str[0].trim();
                String password = str[1].trim();
                set.add(new Manager(userName,password));
            }
        }
        //释放资源
        sc.close();
    }
}
